package com.retallife.user.service.Goods.Impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.retallife.user.dao.Goods.GoodsMapper;
import com.retallife.user.pojo.Goods.*;
import com.retallife.user.service.Goods.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodsServiceImpl implements GoodsService {
    @Autowired
    GoodsMapper goodsMapper;

    @Autowired
    RedisTemplate redisTemplate;

    /**
     * 查询商品
     * @return 是否查询成功
     */
    public List<GoodsVo> FindAllGoods() {
        String GoodName = "所有商品";
        ValueOperations<String,Object> valueOperations= redisTemplate.opsForValue();
        //从redis获取数据 得到key值为GoodName==所有商品的数据
        List<GoodsVo> GoodsVos = (List<GoodsVo>)valueOperations.get("GoodName" + GoodName);
        //如果为空，去数据库获取
        if (CollectionUtils.isEmpty(GoodsVos)){
            GoodsVos = goodsMapper.FindAllGoods();
            //key value相对应的查询
            valueOperations.set("GoodName"+GoodName,GoodsVos);
        }
        return GoodsVos;
    }

    /**
     * 模糊查询某个商品的信息
     * @param GoodsName
     * @return 输出查询数据
     */
    public List<GoodsVo> FindGoodsByName(String GoodsName) {
        return goodsMapper.FindGoodsByName(GoodsName);
    }
}
